//
//  NSString+Format.m
//  YanXiuWang
//
//  Created by 招利 李 on 16/11/23.
//  Copyright © 2016年 小分队. All rights reserved.
//

#import "NSString+Format.h"

void print(NSString * format, ...) {
    va_list args;
#ifdef DEBUG
    va_start(args, format);
    NSString *result = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    printf("%s", [Format(@"\n%@\n", result) cStringUsingEncoding:NSUTF8StringEncoding]);
#else
    va_start(args, format);
    NSLogv(format, args);
    va_end(args);
#endif
}

NSURL * FormatURL(NSString * format, ...) {
    va_list args;
    va_start(args, format);
    NSString *result = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    return [NSURL URLWithString:result];
}

NSString * Format(NSString * format, ...) {
    va_list args;
    va_start(args, format);
    NSString *result = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);
    return result;
}

NSString * FormatTimestamp(NSString * format, NSTimeInterval timestamp) {
    return FormatDate(format, [NSDate dateWithTimeIntervalSince1970:timestamp]);
}

NSString * FormatDate(NSString * format, NSDate *date) {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = format;
    return [formatter stringFromDate:date];
}

@implementation NSString (Easy)

+ (instancetype)stringWithData:(NSData *)data encoding:(NSStringEncoding)encode {
    return [[NSString alloc] initWithData:data encoding:encode];
}

@end

@implementation NSNumber (Equal)

- (NSInteger)length {
    return Format(@"%@", self).length;
}

- (BOOL)isEqualToString:(NSString *)value {
    return [Format(@"%@", self) isEqualToString:value];
}

@end

